home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / mach / hurd / ttyname.c < prev    next >
C/C++ Source or Header  |  1994-02-01  |  3KB  |  109 lines

  1. /* Copyright (C) 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <errno.h>
  20. #include <limits.h>
  21. #include <stddef.h>
  22. #include <dirent.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <hurd.h>
  29. #include <hurd/term.h>
  30. #include <hurd/fd.h>
  31.  
  32. char *__ttyname = NULL;
  33.  
  34. /* Return the pathname of the terminal FD is open on, or NULL on errors.
  35.    The returned storage is good only until the next call to this function.  */
  36. char *
  37. ttyname (int fd)
  38. {
  39.   static const char dev[] = "/dev";
  40.   DIR *dirstream;
  41.   struct dirent *d;
  42.   error_t err;
  43.   mach_port_t fd_cttyid;
  44.   static char nodename[1024] = "";    /* XXX */
  45.   char *name;
  46.   
  47.   /* Open FILENAME relative to DIR and return nonzero iff its ctty ID port
  48.      is the same as FD_CTTYID.  */
  49.   int try (file_t dir, const char *filename)
  50.     {
  51.       mach_port_t file, cttyid;
  52.       if (__USEPORT (CRDIR, __hurd_path_lookup (port, dir, filename, 0, 0,
  53.                         &file)))
  54.     return 0;        /* Can't open it.  */
  55.       err = __term_getctty (file, &cttyid);
  56.       __mach_port_deallocate (__mach_task_self (), file);
  57.       if (err)
  58.     return 0;        /* Not a terminal.  */
  59.       /* We only need to know if CTTYID is the same port as FD_CTTYID,
  60.      so deallocating the reference can never hurt.  */
  61.       __mach_port_deallocate (__mach_task_self (), cttyid);
  62.       return cttyid == fd_cttyid;
  63.     }
  64.  
  65.   /* Get the ctty ID port of the object we want to find.  */
  66.   if (err = HURD_DPORT_USE (fd,
  67.                 (__term_get_nodename (port, nodename),
  68.                  __term_getctty (port, &fd_cttyid))))
  69.     return __hurd_fail (err), NULL;
  70.  
  71.   /* If there was a "nodename" set, and it is correct, return that.  */
  72.   if (nodename[0] != '\0' && __USEPORT (CWDIR, try (port, nodename)))
  73.     return nodename;
  74.  
  75.  
  76.   /* Search the "/dev" directory for a file that returns the same ctty ID
  77.      port that the terminal FD refers to.  */
  78.  
  79.   dirstream = opendir (dev);
  80.   if (dirstream == NULL)
  81.     return NULL;
  82.  
  83.   name = NULL;
  84.   while ((d = readdir (dirstream)) != NULL)
  85.     if (try (dirstream->__port, d->d_name))
  86.       {
  87.     if (__ttyname)
  88.       free (__ttyname);
  89.     __ttyname = malloc (sizeof (dev) + 1 + d->d_namlen);
  90.     if (__ttyname != NULL)
  91.       {
  92.         memcpy (__ttyname, dev, sizeof (dev) - 1);
  93.         __ttyname[sizeof (dev)] = '/';
  94.         memcpy (&__ttyname[sizeof (dev) + 1], d->d_name, d->d_namlen + 1);
  95.       }
  96.     name = __ttyname;
  97.     break;
  98.       }
  99.  
  100.   __mach_port_deallocate (__mach_task_self (), fd_cttyid);
  101.  
  102.   {
  103.     int save = errno;
  104.     (void) closedir (dirstream);
  105.     errno = save;
  106.     return name;
  107.   }
  108. }
  109.